/*************************
Diese Software ist ein Beispiel (sample code) und unterliegt der Microsoft Public License. 
Die Verwendung des Codes ist unter den Bedingungen der Microsoft Public License erlaubt.
*************************
This software is sample code and is subject to the Microsoft Public License. 
You may use this code according to the conditions of the Microsoft Public License.
*************************/
using Egora.Stammportal.LdapAuthorizationService;
using NUnit.Framework;

namespace Egora.stammportal.LdapAuthorizationServiceTest
{
  [TestFixture]
  public class AuthorizationTest
  {
    [Test]
    public void NoApplication()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("dummy", "dummy");
      Assert.IsNotNull(authorizer);
      Assert.IsFalse(authorizer.IsValid);
      Assert.IsFalse(authorizer.IsWeb);
      Assert.IsFalse(authorizer.IsSoap);
    }

    [Test]
    public void AuthorizationSimple()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("http://test.rubicon-it.com",
                                                                                 @"egora1");
      Assert.IsNotNull(authorizer);
      Assert.IsTrue(authorizer.IsValid);
      Assert.AreEqual("egora.eins@egora.at", authorizer.Mail, "MailAddress");
      Assert.AreEqual("&<>\"'ZMR-Behoerdenabfrage_(&GKZ=&1234);", authorizer.Roles, "Roles");
      Assert.AreEqual("Vienna", authorizer.CostCenterId);
      Assert.IsEmpty(authorizer.ChargeCode);
      Assert.AreEqual(600, authorizer.AuthorizationTimeToLive, "TimeToLive");
      Assert.AreEqual(
        "<role value=\"&amp;&lt;&gt;&quot;&apos;ZMR-Behoerdenabfrage_\">\n<param>\n<key>&amp;GKZ</key><value>&amp;1234</value>\n</param>\n</role>",
        authorizer.SoapRoles, "SoapRoles");
      Assert.IsTrue(authorizer.GetAttributeValue("X-AUTHENTICATE-cn-format").EndsWith(" through formatter"));
    }

    [Test]
    public void AuthorizationSimpleNoRole()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("http://testrole.rubicon-it.com",
                                                                                 @"rubicon\peter.grassnigg");
      Assert.IsNotNull(authorizer);
      Assert.IsFalse(authorizer.IsValid);
    }

    [Test]
    public void AuthorizationRecursive()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("http://testr.rubicon-it.com",
                                                                                 @"rubicon\egora2");
      Assert.IsNotNull(authorizer);
      Assert.IsTrue(authorizer.IsValid);
      Assert.AreEqual("egora.zwei@egora.at", authorizer.Mail, "MailAddress");
      Assert.AreEqual("TestRolle;", authorizer.Roles, "Roles");
      Assert.AreEqual(
        "canonical int.rubicon-it.com/Development/egora/egora Zwei",
        authorizer.Ou, "with format.");

      Assert.AreEqual(500, authorizer.AuthorizationTimeToLive, "TimeToLive");

      Assert.AreEqual(
        "<pvpToken version=\"1.9\" xmlns=\"http://egov.gv.at/pvp1.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
        "<authenticate><participantId>Max.Mustermann</participantId>" +
        "<userPrincipal><userId>egora2</userId><cn>egora Zwei</cn><gvOuId></gvOuId><ou>canonical int.rubicon-it.com/Development/egora/egora Zwei</ou><mail>egora.zwei@egora.at</mail><tel>Wien, DW 0815</tel><gvSecClass></gvSecClass><gvGid></gvGid><gvFunction></gvFunction></userPrincipal>" +
        "</authenticate><authorize><role value=\"TestRolle\"></role></authorize></pvpToken>",
        authorizer.PvpSoapFragment.OuterXml, "soap");
    }

    [Test]
    public void NoAuthorizationRecursive()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("http://testnr.rubicon-it.com",
                                                                                 @"egora.drei");
      Assert.IsNotNull(authorizer);
      Assert.IsTrue(authorizer.IsValid);
      Assert.AreEqual("egora.drei@egora.at", authorizer.Mail, "MailAddress");
      Assert.AreEqual("", authorizer.Roles, "Roles");
      Assert.AreEqual("Test", authorizer.Ou, "OU");
    }

    [Test]
    public void OuPathFormatter()
    {
      string path =
        "CN=Test Teresa,OU=OU 2,OU=Soziales,OU=Magistratsabteilung II,OU=Stadt Innsbruck,DC=intra,DC=ibk";
      Stammportal.LdapAuthorizationService.OuPathFormatter formatter = new OuPathFormatter();

      Assert.AreEqual("OU_2/Soziales/Magistratsabteilung_II/Stadt_Innsbruck", formatter.Format(path));
    }

    [Test]
    public void AdditionalAttribute()
    {
      PvpApplicationLdapAuthorizer authorizer = new PvpApplicationLdapAuthorizer("http://test.rubicon-it.com",
                                                                                 @"egora.drei");
      Assert.AreEqual("egora Drei through formatter", authorizer.GetAttributeValue("X-AUTHENTICATE-cn-format"));
    }
  }
}